<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>用户登录</h2>
        <p>请输入您的用户账号和密码</p>
      </div>
      
      <div class="login-form">
        <div class="form-group">
          <label for="user_id">用户账号</label>
          <input 
            type="text" 
            id="user_id" 
            v-model="loginForm.user_id" 
            placeholder="请输入用户账号"
            @keyup.enter="handleLogin"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="loginForm.password" 
            placeholder="请输入密码"
            @keyup.enter="handleLogin"
          />
        </div>
        
        <div class="form-actions">
          <button 
            type="button" 
            class="login-btn" 
            @click="handleLogin"
            :disabled="loading"
          >
            {{ loading ? '登录中...' : '登录' }}
          </button>
          <button 
            type="button" 
            class="register-link-btn" 
            @click="goToRegister"
          >
            没有账号？去注册
          </button>
        </div>
        
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { userAPI } from '@/api/api'

export default {
  name: 'LoginView',
  data() {
    return {
      loginForm: {
        user_id: '',
        password: ''
      },
      loading: false,
      errorMessage: ''
    }
  },
  methods: {
    async handleLogin() {
      if (!this.loginForm.user_id || !this.loginForm.password) {
        this.errorMessage = '请输入用户账号和密码'
        return
      }

      const userId = this.loginForm.user_id
      this.loading = true
      this.errorMessage = ''
      
      try {
        const response = await userAPI.login(userId, this.loginForm.password)
        
        if (response && response.code === 200 && response.data) {
          const user = response.data
          
          // 验证密码
          if (user.password === this.loginForm.password) {
            // 登录成功，保存用户信息到localStorage
            localStorage.setItem('user', JSON.stringify(user))
            localStorage.setItem('isLoggedIn', 'true')
            
            // 跳转到首页
            this.$router.push('/')
          } else {
            this.errorMessage = '密码错误'
          }
        } else {
          this.errorMessage = '用户不存在'
        }
      } catch (error) {
        console.error('登录失败：', error)
        this.errorMessage = '登录失败'
      } finally {
        this.loading = false
      }
    },
    
    goToRegister() {
      this.$router.push('/register')
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  background: white;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  margin-bottom: 8px;
  font-size: 28px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.login-form {
  width: 100%;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 16px;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-group input:focus {
  outline: none;
  border-color: #667eea;
}

.form-group input::placeholder {
  color: #999;
}

.form-actions {
  margin-top: 30px;
}

.login-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  margin-bottom: 15px;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.register-link-btn {
  width: 100%;
  padding: 12px;
  background: transparent;
  color: #667eea;
  border: 2px solid #667eea;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.register-link-btn:hover {
  background: #667eea;
  color: white;
}

.error-message {
  margin-top: 15px;
  padding: 12px;
  background-color: #fee;
  border: 1px solid #fcc;
  border-radius: 6px;
  color: #c33;
  font-size: 14px;
  text-align: center;
}

@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
  }
  
  .login-header h2 {
    font-size: 24px;
  }
}
</style> 